<?php

namespace App\Livewire;

use App\Models\Component as ComponentModel;
use Illuminate\Support\Facades\DB;
use Livewire\Component;
use Filament\Forms\Contracts\HasForms;
use Filament\Forms\Concerns\InteractsWithForms;
use Filament\Actions\Concerns\InteractsWithActions;
use Filament\Actions\Contracts\HasActions;
use Filament\Tables\Contracts\HasTable;
use Filament\Tables\Concerns\InteractsWithTable;
use Filament\Tables\Table;
use Filament\Tables\Columns\TextColumn;
use App\Models\Project;
use Filament\Tables\Columns\ImageColumn;
use Filament\Tables\Actions\Action;
use Filament\Notifications\Notification;

class SavedComponents extends Component implements HasForms, HasActions, HasTable
{
    use InteractsWithForms, InteractsWithActions, InteractsWithTable;

    public $project_id;
    public $save_status;
    public function mount(?string $project_id = null, ?string $save_status = null): void
    {
        $this->project_id = $project_id;
        $this->save_status = $save_status;
    }
    public function render()
    {

        return view('livewire.saved-components');
    }

    public function table(Table $table):Table
    {
        $project_id = $this->project_id;
        $project_title = Project::find($project_id)->title;
        $project_type = Project::find($project_id)->project_type;
        $component_ids = DB::table('component_project_type')->where('project_id', $project_id)->pluck('component_id')->toArray();
        $component_count = count($component_ids);

        return $table->query(ComponentModel::whereIn('id', $component_ids))
            ->heading($project_title .' ( 框架类型：'. $project_type.' , 共包含'. $component_count .'个组件)')
            ->columns([
                 TextColumn::make('title')->label('名称')
                     ->searchable(),
                 ImageColumn::make('screenshot')->label('截图'),
                 TextColumn::make('download_path')->label('项目跟路径')
                     ->searchable(),
            ])
            ->filters([

            ])
            ->actions([
                Action::make('view_detail')
                     ->label('查看详情')
                    ->icon('heroicon-o-eye')
                    ->openUrlInNewTab(),
                Action::make('delete')->label('删除')
                    ->requiresConfirmation()
                    ->modalHeading('删除前确认？')
                    ->modalDescription('确定要从项目中删除该组件吗？')
                   ->icon('heroicon-o-trash')
                   ->action(function(array $arguments){
                       $id = $arguments['id'];
                       $project_id = $arguments['project_id'];

                       DB::table('component_project_type')->where('component_id', $id)->where('project_id', $project_id)->delete();

                       Notification::make('deleted')
                           ->title('成功删除！');
                   })

            ])
            ->headerActions([
                Action::make('project_setting')->label('项目设置')->icon('heroicon-o-cog-6-tooth')->color('gray'),
                Action::make('project_list')->label('我的项目列表')->icon('heroicon-o-list-bullet')->color('gray'),
                Action::make('delete_project')->label('删除项目')->color('danger')->icon('heroicon-o-trash'),
                Action::make('download')->color('gray')->label('下载项目')->icon('heroicon-o-folder-arrow-down'),
            ])
            ->bulkActions([
                //
            ]);
    }
}
